import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { BaseService } from 'src/modules/base.service';
import { Repository } from 'typeorm';
import { CreateRoleDto, UpdateRoleDto } from './role.dto';
import { Role } from './role.entity';
@Injectable()
export class RoleService extends BaseService {
  module: Repository<Role>;
  constructor(
    @InjectRepository(Role)
    private rolesRepository: Repository<Role>,
  ) {
    super();
    this.module = this.rolesRepository;
  }

  async createOrUpdateFn(data) {
    try {
      return await this.rolesRepository.save(data);
    } catch (error) {
      if (error.code && error.code === 'ER_DUP_ENTRY') {
        throw new Error('角色权限字符串已存在');
      }
    }
  }

  async create(data: CreateRoleDto): Promise<any> {
    await this.createOrUpdateFn(data);
  }

  async update(data: UpdateRoleDto): Promise<any> {
    await this.createOrUpdateFn(data);
  }
}
